package dp.ua.epic.test.util;

import java.util.Arrays;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import epic.domain.Hero;
import epic.domain.PathDecision;
import epic.services.HeroService;
import java.util.ResourceBundle;
import static org.mockito.Mockito.*;

/**
 * General utility functions for testing.
 *
 * @author Artem
 */
public class GeneralTestUtil {

    public static String getResourceProperty(String path, String key) {
        return ResourceBundle.getBundle(path).getString(key);
    }

    public static String getPageResourceProperty(Class pageClass, String key) {
        String bundleName = pageClass.getCanonicalName();

        return ResourceBundle.getBundle(bundleName).getString(key);
    }

    public static HeroService createHeroServiceStub() {        
        Hero hero = new Hero("Hero");
        hero.setDecision(PathDecision.RIGHT);
        Hero chuck = new Hero("Chuck");
        chuck.setDecision(PathDecision.STRAIGHT);
        
        HeroService heroServiceMock = mock(HeroService.class);
        when(heroServiceMock.createNewHero(anyString())).thenAnswer(new Answer<Hero>()     {

            @Override
            public Hero answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                String providedHeroName = (String) args[0];
                return new Hero(providedHeroName);
            }
        });

        when(heroServiceMock.setHeroPathDescision(any(Hero.class), any(PathDecision.class))).thenAnswer(new Answer<Hero>()     {

            @Override
            public Hero answer(InvocationOnMock invocation) throws Throwable {
                Object[] args = invocation.getArguments();
                Hero hero = (Hero) args[0];
                PathDecision path = (PathDecision) args[1];
                hero.setDecision(path);
                return hero;
            }
        });
        
        when(heroServiceMock.getAllHeros()).thenReturn(Arrays.asList(hero, chuck));
        return heroServiceMock;
    }
}
